<template>
  <div class="user-container">
    <div class="select-container">
      <el-select
        size="small"
        @change="userchange"
        v-model="value"
        placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </div>
    <div class="list-container">
      <div class="filter">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-col :span="14">
              <span v-if="listState == 1" style="line-height: 33px;padding-left:10px;">已设置管理员列表</span>
              <span v-if="listState == 2" style="line-height: 33px;padding-left:10px;">未设置管理员列表</span>
            </el-col>
          </el-col>
        </el-row>
      </div>
      <div class="data-contant">
        <div>
          <el-table
            :key='tableKey'
            :data="list.records"
            v-loading="loading"
            element-loading-text="给我一点时间"
            size="small"
            stripe
            style="width: 100%;"
            highlight-current-row>
            <el-table-column
              align="center"
              type="index"
              width="80"
              label="编号"
            >
            </el-table-column>
            <!--<el-table-column
              align="center"
              label="用户名称"
              prop="user_name "
            >
            </el-table-column>-->

            <el-table-column
              align="center"
              label="登录名"
              prop="user_loginName"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="用户名称英文"
              prop="user_nameEn"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="性别"
              :formatter="fmtGender"
              prop="user_gender"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="状态"
              :formatter="fmtLength"
              prop="user_status"
            >
            </el-table-column>
            <el-table-column align="center" label="操 作" width="220px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button style="width:85px;" v-if="listState == 2" type="success" size="small" @click="createClick(scope.row)">设置管理员</el-button>
                <el-button style="width:85px;" v-if="listState == 1" type="danger" size="small" @click="handleDelete(scope.row)">撤销管理员</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              layout=" total, prev, pager, next"
              :page-size = "this.postList.rowBounds.limit"
              :total="this.list.count"
              @current-change="current_change"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import '@/styles/newstyle.scss'
  const nameCondition = {
    'property': 'name',
    'value': '',
    'type': 'LIKE'
  }
  const ennameCondition = {
    'property': 'nameEn',
    'value': '',
    'type': 'LIKE'
  }
  export default {
    props: [
      'orgid',
      'count',
      'selectValue'
    ],
    data() {
      return {
        dappendVisible: false,
        updateVisible: false,
        tableKey: 0,
        list: {},
        postList: {
          condition: [],
          rowBounds: {
            offset: 0,
            limit: 10
          }
        },
        loading: false,
        fliter: [
          Object.assign({}, nameCondition),
          Object.assign({}, ennameCondition)
        ],
        appendlist: {},
        options: [
          {
            value: 1,
            label: '已设置人员'
          },
          {
            value: 2,
            label: '未设置人员'
          }
        ],
        value: 1,
        listState: 1,
        updatelist: {},
        deletelist: {}
      }
    },
    created() {
      this.fetchExistUser()
    },
    watch: {
      count(n, o) {
        console.log(n)
        if (n) {
          this.value = 1
          this.fetchExistUser()
        }
      }
    },
    methods: {
      fetchExistUser() {
        this.loading = true
        this.postList.orgId = this.$route.params.orgId
        this.$store.dispatch('SelectExistUser', this.postList).then(res => {
          this.list = res.data
          this.listState = 1
          this.loading = false
        }).catch((err) => {
          console.log(err)
        })
      },
      fetchUnExistUser() {
        this.loading = true
        this.postList.orgId = this.$route.params.orgId
        this.$store.dispatch('SelectNoExistUser', this.postList).then(res => {
          this.list = res.data
          this.listState = 2
          this.loading = false
        }).catch((err) => {
          console.log(err)
        })
      },
      appendData() {
        this.$store.dispatch('SysOrgAdmin', this.appendlist).then(res => {
          this.fetchUnExistUser()
        }).catch((err) => {
          console.log(err)
        })
      },
      deleteData() {
        this.$store.dispatch('sysOrgAdminDELETE', this.deletelist).then(res => {
          this.fetchExistUser()
        }).catch((err) => {
          console.log(err)
        })
      },
      fmtLength(row, column) {
        const arr = row[column.property]
        /* 这里的 if(arr === undefined)
         * 是根据自己的需求，决定arr长度为0时的判断条件
         * 可以和这个不同
         */
        if (arr === '0') {
          return '活动'
        } else if (arr === '1') {
          return '禁用'
        } else if (arr === '2') {
          return '删除'
        }
      },
      fmtGender(row, column) {
        const arr = row[column.property]
        if (arr === '0') {
          return '未知'
        } else if (arr === '1') {
          return '男'
        } else if (arr === '2') {
          return '女'
        }
      },
      handleDelete(row) {
        this.$confirm('是否撤销管理员?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deletelist.id = row.id
          this.deleteData()
          this.$message({
            type: 'success',
            message: '撤销成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      createClick(row) {
        this.$confirm('是否配置为管理员?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.appendlist.id = row.user_id
          this.appendlist.orgId = this.$route.params.orgId
          this.appendlist.userId = this.$store.getters.userid
          this.appendlist.fcUser = this.$store.getters.userid
          this.appendData()
          this.$message({
            type: 'success',
            message: '配置管理员成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      current_change(currentPage) { // 通过分页获取数据
        const page = currentPage - 1
        this.postList.rowBounds.offset = page * 10
        if (this.listState === 1) {
          this.fetchExistUser()
        } else {
          this.fetchUnExistUser()
        }
      },
      /*
      *  查询
      * */
      userchange(val) {
        if (val === 1) {
          this.fetchExistUser()
        } else {
          this.fetchUnExistUser()
        }
      },
      title_change(type) { // 分类数据
        if (type === null) {
          const nullfliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].property === 'name') {
              nullfliter.splice(i, 1)
              this.fliter[i].value = ''
            }
          }
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].value === '') {
              continue
            }
            condition.push(nullfliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        } else {
          const conditionTimefliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].property === 'name') {
              conditionTimefliter[i].value = type
              this.fliter[i].value = type
            }
          }
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].value === '') {
              continue
            }
            condition.push(conditionTimefliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        }
      },
      entitle_change(type) { // 分类数据
        if (type === null) {
          const nullfliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].property === 'nameEn') {
              nullfliter.splice(i, 1)
              this.fliter[i].value = ''
            }
          }
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].value === '') {
              continue
            }
            condition.push(nullfliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        } else {
          const conditionTimefliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].property === 'nameEn') {
              conditionTimefliter[i].value = type
              this.fliter[i].value = type
            }
          }
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].value === '') {
              continue
            }
            condition.push(conditionTimefliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        }
      }
    }
  }
</script>

<style scoped>
  .user-container{
    height: 100%;
    overflow-y: auto;
  }
  .container{
    padding: 20px;
    height: 100%;
    overflow-y: auto;
  }
  .select-container{
    padding: 20px 10px 0;
    border-radius: 4px;
  }
  .list-container{
    margin-top:10px;
    padding:0 10px 10px 10px;
    border-radius: 4px;
  }
  .data-contant{
    padding: 30px 10px 20px;
  }
  .data-detail{
    font-size: 14px;
    padding:20px 0;
    text-align: center;
  }
  .data-title{
    font-size: 14px;
    padding:10px 0;
    text-align: center;
  }
  .data-title div{
    font-size: 14px;
    padding:5px 0;
  }
  .filter{
    padding: 10px 5px;
    border-bottom: 2px solid#e4e7ed;
  }
  .list-container{
  }
  .bold{
    font-weight: bold;
  }
</style>
